package com.leetcode.p0;

import com.utils.DataUtils;

public class Test88 {

    public static void swap(int[] arr, int i, int j) {
        int t = arr[i];
        arr[i] = arr[j];
        arr[j] = t;
    }

    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int len1 = m - 1;
        int len2 = n - 1;

        int currentPosition = m + n - 1;

        while (len1 >= 0 && len2 >= 0) {
            if (nums1[len1] > nums2[len2]) {
                swap(nums1, len1, currentPosition);
                len1--;
            } else {
                nums1[currentPosition] = nums2[len2];
                len2--;
            }
            currentPosition--;
        }

        while (len2 >= 0 && currentPosition >= 0) {
            nums1[currentPosition] = nums2[len2];
            len2--;
            currentPosition--;
        }
    }

    public static void main(String[] args) {
        Test88 test = new Test88();
//        int[] nums1 = new int[]{1, 1, 9, 10, 0, 0, 0};
//        int[] nums2 = new int[]{1, 4, 11};
//        test.merge(nums1, 4, nums2, 3);
//        DataUtils.printArray(nums1);

        int[] nums1 = new int[]{2, 3, 0};
        int[] nums2 = new int[]{1};
        test.merge(nums1, 2, nums2, 1);
        DataUtils.printArray(nums1);
    }

}
